import Axios from "axios";

class httpRequest {
  // 请求拦截
  interceptors(instance) {
    // 添加请求拦截器
    instance.interceptors.request.use(
      (config) => {
        return config;
      },
      (error) => {
        return Promise.reject(error);
      }
    );

    // 添加响应拦截器
    instance.interceptors.response.use(
      (res) => {
        const { data } = res;
        return data;
      },
      (error) => {
        return Promise.reject(error);
      }
    );
  }

  // 创建实例
  create() {
    let conf = {
      headers: {
        "Content-Type": "application/json; charset=utf-8",
        "X-URL-PATH": location.pathname,
      },
      timeout: 0,
    };
    return Axios.create(conf);
  }

  // 请求实例
  request(options) {
    var instance = this.create();
    this.interceptors(instance, options.url);
    options = Object.assign({}, options);
    return instance(options);
  }
}
export default httpRequest;
